//
//  HomeTableViewController.m
//  NetEase
//
//  Created by Apple on 2018/5/5.
//  Copyright © 2018年 techbird. All rights reserved.
//

#import "HomeTableViewController.h"
#import "NetworkManager.h"
#import "YYModel.h"
#import "NewsModel.h"
#import "NewsTableViewCell.h"
#import "SliderView.h"
#import "NewsDetailViewController.h"

@interface HomeTableViewController ()
@property(strong, nonatomic) NSArray<NewsModel *> *newsList;
@property(strong, nonatomic) SliderView *sliderView;
@end

@implementation HomeTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self loadData];
}

- (void)setupUI{
    [self.tableView registerNib:[UINib nibWithNibName:@"NewsTableViewCell" bundle:nil] forCellReuseIdentifier:@"NewsTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"BigCell" bundle:nil] forCellReuseIdentifier:@"BigCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ImageCell" bundle:nil] forCellReuseIdentifier:@"ImageCell"];
    [self setupSliderView];
}

- (void)setupSliderView{
    self.sliderView = [[SliderView alloc]init];
    UIScreenEdgePanGestureRecognizer *gesture = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureRecognizer:)];
    gesture.edges = UIRectEdgeLeft;
    [self.view addGestureRecognizer:gesture];
    
    // 禁止tableView接收这个手势
    [self.tableView.panGestureRecognizer requireGestureRecognizerToFail:gesture];
}

- (void)panGestureRecognizer:(UIScreenEdgePanGestureRecognizer *)gesture{
      // 获取实时滑动到的点
    CGPoint offsetPoint = [gesture translationInView:gesture.view];
     // 偏移清零：为了设置transform设置的
    [gesture setTranslation:CGPointZero inView:gesture.view];
    switch (gesture.state) {
        case UIGestureRecognizerStateChanged:
        {
            self.sliderView.transform = CGAffineTransformTranslate(self.sliderView.transform, offsetPoint.x, 0);
        }
            break;
        case UIGestureRecognizerStateEnded:
        case UIGestureRecognizerStateCancelled:
        case UIGestureRecognizerStateFailed:
        {
            CGFloat sliderX;
            if (CGRectGetMaxX(self.sliderView.frame) > [UIScreen mainScreen].bounds.size.width*0.7){
                sliderX = 0;
            }else{
                sliderX = -[UIScreen mainScreen].bounds.size.width;
            }
            [UIView animateWithDuration:0.2 animations:^{
               self.sliderView.frame = CGRectMake(sliderX, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
            }];
        }
            break;
        default:
            break;
    }
}

- (void)loadData{
    NSString *URLString = @"http://c.m.163.com/nc/article/list/T1348648141035/0-20.html";
    [[NetworkManager sharedManager] GETWithURLString:URLString parameters:nil success:^(NSDictionary *responseObject) {
        // 动态的取出responseObject字典的第一个key
        NSString *key = responseObject.keyEnumerator.nextObject;
        // 取出responseObject字典里面的字典数组
        NSArray *dictArr = responseObject[key];
        self.newsList = [NSArray yy_modelArrayWithClass:[NewsModel class] json:dictArr];
        [self.tableView reloadData];
        NSLog(@"dictArr %@",dictArr);
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.newsList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NewsModel *item =  self.newsList[indexPath.row];
    NSString *identifier;
    if (item.imgType){
        identifier = @"BigCell";
    }else if(item.imgextra.count>0) {
        identifier = @"ImageCell";
    }else{
        identifier = @"NewsTableViewCell";
    }
    
    NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    cell.newsModel = item;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NewsModel *item =  self.newsList[indexPath.row];
    if (item.imgType){
        return 180;
    }else if(item.imgextra.count>0) {
        return 130;
    }else{
        return 80;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NewsModel *newsModel =  self.newsList[indexPath.row];
    NewsDetailViewController *vc = [[NewsDetailViewController alloc]initWithNewsModel:newsModel];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
